﻿<template>
  <div class="cRMCustomer-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="13" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="客户名称">
              <el-input v-model="queryParams.custName" clearable="" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="手机号码">
              <el-input v-model="queryParams.mobilePhone" clearable="" placeholder="请输入手机号码"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="电话号码">
              <el-input v-model="queryParams.telephone" clearable="" placeholder="请输入电话号码"/>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
          </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
              <el-form-item label="客户来源">
                    <el-select v-model="queryParams.customerSource" placeholder="客户来源" clearable>
                      <el-option label="抖音" :value="1" />
                      <el-option label="快手" :value="2" />
                      <el-option label="小红书" :value="3" />
                      <el-option label="百度搜索" :value="4" />
                      <el-option label="广告" :value="5" />
                      <el-option label="转介绍" :value="6" />
                      <el-option label="线上询价" :value="7" />
                      <el-option label="预约上门" :value="8" />
                      <el-option label="陌拜" :value="9" />
                    </el-select>
               </el-form-item>
            </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="客户状态">
              <el-select v-model="queryParams.customerStatus" placeholder="客户状态" clearable>
                     <el-option label="跟进中" :value="1" />
                     <el-option label="有意向" :value="2" />
                     <el-option label="无回复" :value="3" />
                     <el-option label="无意向" :value="4" />
                     <el-option label="已成交" :value="5" />
               </el-select>
            </el-form-item>
          </el-col>

          
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item >
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'cRMCustomer:page'"> 查询 </el-button>
                      <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                        <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI" style="margin-left:5px;"> 高级查询 </el-button>
                        <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI" style="margin-left:5px;"> 隐藏 </el-button>
                <!-- <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="openAddCRMCustomer" v-auth="'cRMCustomer:add'"> 新增客户 </el-button> -->
              </el-button-group>
            </el-form-item>
            
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
                				row-key="id"
                @sort-change="sortChange"
				border="">
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="custName" label="客户名称"  show-overflow-tooltip=""  >
            <template #default="scope">
              <el-button  size="small" text="" type="primary" @click="openDetailCRMCustomer(scope.row)" > {{scope.row.custName}} </el-button>
            </template>
        </el-table-column>
        <!-- <el-table-column prop="custLeadId" label="客户负责人"  show-overflow-tooltip="" /> -->
        <!-- <el-table-column prop="isCollect" label="是否关注"  show-overflow-tooltip="" /> -->
        <el-table-column prop="customerSource" label="客户来源"  show-overflow-tooltip="" >
          <template #default="scope">
              <el-tag v-if="     scope.row.customerSource === 1 " type="danger">抖音</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 2 " type="danger">快手</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 3 " type="danger">小红书</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 4 " type="danger">百度搜索</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 5 " type="danger">广告</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 6 " type="danger">转介绍</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 7 " type="danger">线上询价</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 8 " type="danger">预约上门</el-tag>
              <el-tag v-else-if="scope.row.customerSource === 9 " type="danger">陌拜</el-tag>
            </template>
        </el-table-column>
        
        
        <el-table-column prop="contactName" label="首要联系人"  show-overflow-tooltip="" />
        <el-table-column prop="mobilePhone" label="手机号码"  show-overflow-tooltip="" />
        <el-table-column prop="telephone" label="电话号码"  show-overflow-tooltip="" />
        <el-table-column prop="customerLevel" label="客户级别"  show-overflow-tooltip="" >
          <template #default="scope">
              <el-tag v-if="     scope.row.customerLevel === 1 "   >抖音</el-tag>
              <el-tag v-else-if="scope.row.customerLevel === 2 "   >快手</el-tag>
              <el-tag v-else-if="scope.row.customerLevel === 3 "   >小红书</el-tag>
            </template>
        </el-table-column>
       
        <el-table-column prop="customerStatus" label="客户状态"  show-overflow-tooltip="" >
          <template #default="scope">
                <el-tag v-if="     scope.row.customerStatus === 1 "  type="success"  >跟进中</el-tag>
                <el-tag v-else-if="scope.row.customerStatus === 2 "  type="success"  >有意向</el-tag>
                <el-tag v-else-if="scope.row.customerStatus === 3 "  type="success"  >无回复</el-tag>
                <el-tag v-else-if="scope.row.customerStatus === 4 "  type="success"  >无意向</el-tag>
                <el-tag v-else-if="scope.row.customerStatus === 5 "  type="success"  >已成交</el-tag>
              </template>
        </el-table-column>

        <el-table-column prop="customerSpecification" label="客户规格"  show-overflow-tooltip="" >
          <template #default="scope">
                <el-tag v-if="     scope.row.customerSpecification === 1 "  type="warning"  >初创企业</el-tag>
                <el-tag v-else-if="scope.row.customerSpecification === 2 "  type="warning"  >中小企业</el-tag>
                <el-tag v-else-if="scope.row.customerSpecification === 3 "  type="warning"  >大型企业</el-tag>
              </template>
        </el-table-column>

        <!-- <el-table-column prop="nextContactDate" label="下次联系时间"  show-overflow-tooltip="" /> -->
        <!-- <el-table-column prop="province" label="所在省"  show-overflow-tooltip="" />
        <el-table-column prop="city" label="所在市"  show-overflow-tooltip="" />
        <el-table-column prop="district" label="区县"  show-overflow-tooltip="" /> -->

        <!-- <el-table-column prop="region" label="区域"  show-overflow-tooltip="" width="150" /> -->
        <el-table-column prop="detailedAddress" label="详细地址"  show-overflow-tooltip="" />
        <el-table-column prop="lastFollowUpRecord" label="最后跟进记录"  show-overflow-tooltip="" />
        <el-table-column prop="lastFollowUpTime" label="最后跟进时间"  show-overflow-tooltip="" />
        <el-table-column prop="remark" label="备注"  show-overflow-tooltip="" />
        <!-- <el-table-column prop="orderNo" label="排序"  show-overflow-tooltip="" /> -->
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('cRMCustomer:update') || auth('cRMCustomer:delete')">
          <template #default="scope">
            <el-button icon="ele-Finished" size="small" text="" type="primary" @click="openClaimCustomer(scope.row)" v-auth="'cRMCustomer:update'"> 认领 </el-button>
            <!--Finished <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delCRMCustomer(scope.row)" v-auth="'cRMCustomer:delete'"> 删除 </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				size="small"
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <printDialog
        ref="printDialogRef"
        :title="printCRMCustomerTitle"
        @reloadTable="handleQuery" />
      <editDialog
        ref="editDialogRef"
        :title="editCRMCustomerTitle"
        fullscreen
        @reloadTable="handleQuery"
      />
      <detailDialog
        ref="detailDialogRef"
        :title="detailCRMCustomerTitle"
        @reloadTable="handleQuery"
      />

    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="cRMCustomer">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';
  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views/main/cRMCustomer/component/editDialog.vue'
  import detailDialog from '/@/views/main/cRMCustomer/component/detailDialog.vue'
  import { pageCRMCustomer, deleteCRMCustomer } from '/@/api/main/cRMCustomer';

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const detailDialogRef = ref();

  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    field: 'createTime', // 默认的排序字段
    order: 'descending', // 排序方向
    descstr: 'descending', // 降序排序的关键字符
    total: 0 as any,
  });

  const printCRMCustomerTitle = ref("");
  const editCRMCustomerTitle = ref("");
  const detailCRMCustomerTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }

  // 查询操作
  const handleQuery = async () => {
    loading.value = true;

    //查询类型
    queryParams.value.selectType = 2;
    var res = await pageCRMCustomer(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 列排序
  const sortChange = async (column: any) => {
    tableParams.value.field = column.prop;
    tableParams.value.order = column.order;
    await handleQuery();
  };

  // 认领
  const openClaimCustomer = () => {
    ElMessage.success("功能开发中。。。");
  };

  // 打开客户详情
  const openDetailCRMCustomer = async (row: any) => {
    detailCRMCustomerTitle.value = 'CRM客户详情';
    detailDialogRef.value.openDrawer(row);
  }


  // 删除
  const delCRMCustomer = (row: any) => {
    ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(async () => {
    await deleteCRMCustomer(row);
    handleQuery();
    ElMessage.success("删除成功");
  })
  .catch(() => {});
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  handleQuery();
</script>
<style scoped>
:deep(.el-input),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

